class SpiralMatrix {
	public int[][] generateMatrix(int n) {
		int[][] nums= new int[n][n];
		int startx = 0;
		int starty = 0;
		int offset = 1;
		int count = 1;
		int loop = 0;
		int x,y;
		
		while(loop < n/2){
			for(y = starty ; y < n - offset ; y++){
				nums[startx][y] = count++;
			}
			for(x = startx ; x < n - offset ; x++){
				nums[x][y] = count++;
			}
			for( ; y > starty ; y--){
				nums[x][y] = count++;
			}
			for( ; x > startx ; x--){
				nums[x][y] = count++;
			}
			startx++;
			starty++;
			offset++;
			loop++;
			if (n % 2 == 0){
				nums[n/2][n/2] = count;
			}
		}
		return nums;
	}
}